package com.shadow.shadow_lib.assistmove;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

public class BaseMoveListener extends onMoveListener {
	private ViewGroup partent_of_moveview = null;
	private LayoutParams partent_layoutParams = null;

	private WindowManager.LayoutParams wlp;
	private WindowManager mWindowManager;

	public BaseMoveListener(Activity activity) {
		mWindowManager = activity.getWindowManager();
		wlp = new WindowManager.LayoutParams();
		wlp.width = LayoutParams.WRAP_CONTENT;
		wlp.height = LayoutParams.WRAP_CONTENT;
		wlp.gravity = Gravity.TOP | Gravity.LEFT;
		wlp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		wlp.format = PixelFormat.TRANSLUCENT;
		wlp.format = PixelFormat.RGBA_8888;
	}

	float startX;
	float startY;

	float localX;
	float localY;

	@Override
	public void onEventStart(View view, MotionEvent event) {
		partent_of_moveview = (ViewGroup) view.getParent();
		startX = event.getRawX();
		startY = event.getRawY();

		int[] location = new int[2];
		view.getLocationOnScreen(location);

		localX = wlp.x = (int) (event.getRawX() - event.getX());
		localY = wlp.y = (int) (event.getRawY() - event.getY());

		partent_of_moveview.removeView(view);
		partent_layoutParams = view.getLayoutParams();
		mWindowManager.addView(view, wlp);
	}

	@Override
	public void onEventMoving(View view, MotionEvent event) {

		wlp.x = (int) (localX + event.getRawX() - startX);
		wlp.y = (int) (localY + event.getRawY() - startY);
		mWindowManager.updateViewLayout(view, wlp);
	}

	@Override
	public void onEventStop(View view, MotionEvent event) {
		mWindowManager.removeView(view);
		partent_of_moveview.addView(view, partent_layoutParams);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return super.onTouch(v, event);
	}
}
